<?php
/**
 * Created by PhpStorm.
 * User: chapin
 * Date: 2019-01-15
 * Time: 11:17
 */
header("Content-type: text/html; charset=utf-8");
defined('BASEPATH') OR exit('No direct script access allowed');

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Xls;

require APPPATH . 'libraries/REST_Controller.php';


class Collection extends REST_Controller
{
	/**
	 * 取数 取前一天
	 */
	public function qushu_post()
	{
		$data = $this->post('data');
		echo $data;
		$data = (array)json_decode($data);

		$data_r = [
			'date_time' => $data['data'][0],
			'pay_amt' => str_replace(',', '', $data['data'][1]),
			'visitor' => str_replace(',', '', $data['data'][2]),
			'custo_price' => str_replace(',', '', $data['data'][3]),
			'pay_conver_rate' => str_replace(',', '', $data['data'][4]),
			'pay_item_num' => str_replace(',', '', $data['data'][5]),
			'pay_num' => str_replace(',', '', $data['data'][6]),
			'pay_buy_num' => str_replace(',', '', $data['data'][7]),
			'collect_num' => str_replace(',', '', $data['data'][8]),
			'collect_buy_num' => str_replace(',', '', $data['data'][9]),
			'shop_collect_buy_num' => str_replace(',', '', $data['data'][10]),
			'jump_leave_rate' => str_replace(',', '', $data['data'][11]),
			'aver_leng_of_stay' => str_replace(',', '', $data['data'][12]),
			'pageviews' => str_replace(',', '', $data['data'][13]),
			'per_capit_pageviews' => str_replace(',', '', $data['data'][14]),
			'desc_match_score' => str_replace(',', '', $data['data'][15]),
			'service_atti_score' => str_replace(',', '', $data['data'][16]),
			'logist_services_score' => str_replace(',', '', $data['data'][17]),
			'negat_commen_num' => str_replace(',', '', $data['data'][18]),
			'succe_refun_amt' => str_replace(',', '', $data['data'][19]),
			'add_purcha_num' => str_replace(',', '', $data['data'][20]),
			'add_purcha_peo_num' => str_replace(',', '', $data['data'][21]),
			'old_visitor' => str_replace(',', '', $data['data'][22]),
			'new_visitor' => str_replace(',', '', $data['data'][23]),
			'throu_train_pay' => str_replace(',', '', $data['data'][24]),
			'drill_exhib_pay' => str_replace(',', '', $data['data'][25]),
			'taobao_pay' => str_replace(',', '', $data['data'][26]),
			'pinxiaobao_pay' => "",
			'tot_pay' => ($data['data'][24]+$data['data'][25]+$data['data'][26]),
			'desc_same_aver' => "",
			'service_same_aver' => "",
			'logistic_same_aver' => "",
            'store' => $data['store'],
		];

		$result = $this->db->where('store', $data['store'])->where('date_time', $data_r['date_time'])->get('day_take_num')->row_array();
		
		if (empty($result)) {
			$info = $this->db->insert('day_take_num', $data_r);
		} else {
			$info = $this->db->where('store', $data['store'])->where('date_time', $data_r['date_time'])->update('day_take_num', $data_r);
		}
        var_dump($info);
		return $this->response($info, REST_Controller::HTTP_OK);
	}

	/**
	 * 大盘数据 取前一天
	 */
	public function shichang_post()
	{
		$data = $this->post('data');
		echo $data;
		$data = (array)json_decode($data);

		$data_r = [
			'visitor' => $data['fangke'],
			'add_purcha_peo_num' => $data['jiagou'],
			'collect_peo_num' => $data['shoucang'],
			'date_time' => $data['date']
		];

		$result = $this->db->where('date_time', $data['date'])->get('big_plat')->row_array();

		if (empty($result)) {
			$info = $this->db->insert('big_plat', $data_r);
		} else {
			$info = $this->db->where('date_time', $data['date'])->update('big_plat', $data_r);
		}

		return $this->response($info, REST_Controller::HTTP_OK);

	}

	/**
	 * 商品 获取cookie
	 * @throws \PhpOffice\PhpSpreadsheet\Exception
	 * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
	 */
	public function shangping_post()
	{
        $data = $this->post('data');
        echo $data;
        $data = (array)json_decode($data);
		$date_time = $data['date'];
		$url = "https://sycm.taobao.com/bda/download/excel/items/effect/ItemEffectExcel.do?dateRange=".$date_time."|".$date_time."&dateType=recent1&orderDirection=false&orderField=itemPv&type=0&device=";
        $store = $data['store'];
        $cookie = $this->db->where('store', $store)->order_by('id', 'DESC')->get('cookie_data')->row_array();

		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_COOKIE, $cookie['cookie_str']);
		$output = curl_exec($ch);
		curl_close($ch);

		$filename = 'data/'.$store.'shangpin'.$date_time.'.xls';

		if (empty($output)) {
			return $this->set_response([
				'status' => FALSE,
				'message' => '空数据'
			], REST_Controller::HTTP_OK);
		}

		//检查文件是否存在
		if (file_exists($filename)) {

			return $this->set_response([
				'status' => FALSE,
				'message' => '重复请求'
			], REST_Controller::HTTP_OK);

		} else {
			if (file_put_contents($filename, $output, FILE_APPEND)) {

				$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
				$reader->setReadDataOnly(true);
				$spreadsheet = $reader->load($filename);
				$sheet = $spreadsheet->getActiveSheet();
				$highestRow = $sheet->getHighestRow();  // 最大行数
				$highestColumn = $sheet->getHighestColumn(); // 最大列数
				$highestColumn = "AD";
				// 把下标变成数字,如A-I分别对应1-30
				$highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);

				if($highestRow <= 5){ // 因为students.xlsx表格数据是从第五行开始的
					return $this->set_response([
						'status' => FALSE,
						'message' => '写入失败'
					], REST_Controller::HTTP_OK);
				}

				//组装数据数组
				$data = array();
				for($row = 5;$row <= $highestRow;$row++){
					$tempData['belong_terminal'] = $sheet->getCellByColumnAndRow(1, $row)->getValue(); //所属终端
					$tempData['item_id'] = $sheet->getCellByColumnAndRow(2, $row)->getValue(); //商品id
					$tempData['item_title'] = $sheet->getCellByColumnAndRow(3, $row)->getValue(); //商品标题
					$tempData['item_online_status'] = $sheet->getCellByColumnAndRow(4, $row)->getValue(); //商品在线状态
					$tempData['item_url'] = $sheet->getCellByColumnAndRow(5, $row)->getValue(); //商品链接
					$tempData['pageviews'] = $sheet->getCellByColumnAndRow(6, $row)->getValue(); //浏览量
					$tempData['visitor'] = $sheet->getCellByColumnAndRow(7, $row)->getValue(); //访客数
					$tempData['aver_leng_of_stay'] = $sheet->getCellByColumnAndRow(8, $row)->getValue(); //平均停留时长
					$tempData['detai_page_bounc_rate'] = $sheet->getCellByColumnAndRow(9, $row)->getValue(); //详情页跳出率
					$tempData['order_conver_rate'] = $sheet->getCellByColumnAndRow(10, $row)->getValue(); //下单转化率
					$tempData['order_pay_conver_rate'] = $sheet->getCellByColumnAndRow(11, $row)->getValue(); //下单支付转化率
					$tempData['pay_conver_rate'] = $sheet->getCellByColumnAndRow(12, $row)->getValue(); //支付转化率
					$tempData['order_amt'] = $sheet->getCellByColumnAndRow(13, $row)->getValue(); //下单金额
					$tempData['order_item_num'] = $sheet->getCellByColumnAndRow(14, $row)->getValue(); //下单商品件数
					$tempData['order_buy_num'] = $sheet->getCellByColumnAndRow(15, $row)->getValue(); //下单买家数
					$tempData['pay_amt'] = $sheet->getCellByColumnAndRow(16, $row)->getValue(); //支付金额
					$tempData['pay_item_num'] = $sheet->getCellByColumnAndRow(17, $row)->getValue(); //支付商品件数
					$tempData['add_purcha_num'] = $sheet->getCellByColumnAndRow(18, $row)->getValue(); //加购件数
					$tempData['visitor_aver_value'] = $sheet->getCellByColumnAndRow(19, $row)->getValue(); //访客平均价值
					$tempData['click_num'] = $sheet->getCellByColumnAndRow(20, $row)->getValue(); //点击次数
					$tempData['click_rate'] = $sheet->getCellByColumnAndRow(21, $row)->getValue(); //点击率
					$tempData['exposur'] = $sheet->getCellByColumnAndRow(22, $row)->getValue(); //曝光量
					$tempData['collect_peo_num'] = $sheet->getCellByColumnAndRow(23, $row)->getValue(); //收藏人数
					$tempData['search_lead_to_pay_buyer_num'] = $sheet->getCellByColumnAndRow(24, $row)->getValue(); //搜索引导支付买家数
					$tempData['custo_price'] = $sheet->getCellByColumnAndRow(25, $row)->getValue(); //客单价
					$tempData['search_buy_rate'] = $sheet->getCellByColumnAndRow(26, $row)->getValue(); //搜索支付转化率
					$tempData['search_lead_visitor'] = $sheet->getCellByColumnAndRow(27, $row)->getValue(); //搜索引导访客数
					$tempData['pay_buy_num'] = $sheet->getCellByColumnAndRow(28, $row)->getValue(); //支付买家数
					$tempData['afte_sale_succe_refun_amt'] = $sheet->getCellByColumnAndRow(29, $row)->getValue(); //售中售后成功退款金额
					$tempData['afte_sale_succe_refun_num'] = $sheet->getCellByColumnAndRow(30, $row)->getValue(); //售中售后成功退款笔数
					$tempData['item_coding'] = ""; //商品编码 在匹配数据表按照id查找
					$tempData['year_season'] = ""; //年份季节 在匹配数据表按照id查找
					$tempData['positive_price'] = ""; //正特价 根据年份季节设置条件匹配
					$tempData['tag_price'] = ""; //吊牌价 根据匹配数据表按照id查找
					$tempData['tag_amt'] = ""; //吊牌额 支付商品件数*吊牌价
					$tempData['date_time'] = date("Y-m-d 00:00:00", strtotime("-1 day")); //日期
                    $tempData['store'] = $store;
					$data[] = $tempData;
				}

				//添加数据库
				$this->db->insert_batch('item_effect', $data);

				//批量修改商品编码，年份季节，吊牌价，吊牌额字段
				$effect_match = $this->db->select('a.pay_item_num,b.item_id,b.item_coding,b.year_season,b.tag_price')->from('item_effect as a')->join('match_data as b', 'a.item_id = b.item_id')->where('a.date_time', date("Y-m-d 00:00:00", strtotime("-1 day")))->get()->result_array();
				$update_r = [];
				foreach($effect_match as $key => $value) {
					$update_r[] = [
						'item_id' => $value['item_id'],
						'item_coding' => $value['item_coding'],
						'year_season' => $value['year_season'],
						'tag_price' => $value['tag_price'],
						'tag_amt' => $value['pay_item_num'] * $value['tag_price'],
					];
				}
				$this->db->update_batch('item_effect', $update_r, 'item_id');

				//根据条件设置正特价
				$positive_price = $this->db->where('module', "库存")->get('setting')->result_array();
				//填充正特价字段
				foreach ($positive_price as $key => $value) {
					if ($value['last_condit'] != '其他') {
						$this->db->query('update `item_effect` set `positive_price` = "'.$value['result'].'" where `year_season` '.$value['judge'].' "'.$value['last_condit'].'" and `date_time` = "'.date("Y-m-d 00:00:00", strtotime("-1 day")).'"');
					} else {
						$this->db->query('update `item_effect` set `positive_price` = "'.$value['result'].'" where `positive_price` = "" and `date_time` = "'.date("Y-m-d 00:00:00", strtotime("-1 day")).'"');
					}

				}

				return $this->response(true, REST_Controller::HTTP_OK);
			}
		}

		return $this->response(false, REST_Controller::HTTP_OK);

	}

	/**
	 * 赤兔数据 取前两天
	 */
	public function chitu_post()
	{
		$data = $this->post('data');
		echo $data;
        $data = json_decode(str_replace('\n', '', $data));

		foreach ($data as $key => $value) {
		    if (is_object($value)) {
                $data_r[] = (array)$data[$key];
            }else{
                $data_r[] = $value;
            }
		}
        $store = preg_replace("/ /","",$data_r[2]);
        $store = preg_replace("/  /","",$store);
		$data_t[0] = [
			'advisory_peo_num' => str_replace(['￥',',',' '], '', $data_r[0]['咨询人数']),
			'cust_service_sale_pay' => str_replace(['￥',',',' '], '', $data_r[0]['客服销售额']),
			'tomorrow_pay_succe_rate' => str_replace(['￥',',',' '], '', $data_r[0]['询单->次日付款成功率']),
			'wangwang_reply_rate' => str_replace(['￥',',',' '], '', $data_r[0]['旺旺回复率']),
			'aver_response_time' => str_replace(['￥',',',' '], '', $data_r[0]['平均响应时间']),
			'slow_response_peo_num' => str_replace(['￥',',',' '], '', $data_r[0]['慢响应人数']),
			'long_receptio_peo_num' => str_replace(['￥',',',' '], '', $data_r[0]['长接待人数']),
			'date_time' => str_replace(['￥',',',' '], '', $data_r[0]['日期']),
            'store' => $store
		];
		$data_t[1] = [
			'advisory_peo_num' => str_replace(['￥',',',' '], '', $data_r[1]['咨询人数']),
			'cust_service_sale_pay' => str_replace(['￥',',',' '], '', $data_r[1]['客服销售额']),
			'tomorrow_pay_succe_rate' => str_replace(['￥',',',' '], '', $data_r[1]['询单->次日付款成功率']),
			'wangwang_reply_rate' => str_replace(['￥',',',' '], '', $data_r[1]['旺旺回复率']),
			'aver_response_time' => str_replace(['￥',',',' '], '', $data_r[1]['平均响应时间']),
			'slow_response_peo_num' => str_replace(['￥',',',' '], '', $data_r[1]['慢响应人数']),
			'long_receptio_peo_num' => str_replace(['￥',',',' '], '', $data_r[1]['长接待人数']),
			'date_time' => str_replace(['￥',',',' '], '', $data_r[1]['日期']),
            'store' => $store
		];

		$result1 = $this->db->where('store', $data_r[2])->where('date_time', $data_t[0]['date_time'])->get('chitu_data')->result_array();
		$result2 = $this->db->where('store', $data_r[2])->where('date_time', $data_t[1]['date_time'])->get('chitu_data')->result_array();
		if (empty($result1)) {
			$this->db->insert('chitu_data', $data_t[0]);
		} else {
			$this->db->where('store', $data_r[2])->where('date_time', $data_t[0]['date_time'])->update('chitu_data', $data_t[0]);
		}

		if (empty($result2)) {
			$info = $this->db->insert('chitu_data', $data_t[1]);
		} else {
			$info = $this->db->where('store', $data_r[2])->where('date_time', $data_t[1]['date_time'])->update('chitu_data', $data_t[1]);
		}

		return $this->response(true, REST_Controller::HTTP_OK);

	}

	/**
	 * 库存 存为当天日期
	 * @throws \PhpOffice\PhpSpreadsheet\Exception
	 * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
	 */
	public function stock_post()
	{
		$data = $this->post('data');
		echo $data;
		$data = (array)json_decode($data);
        $store = $data['store'];

		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$data['href']);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		$output = curl_exec($ch);
		curl_close($ch);

		$filename = 'data/'.$store.'kucun'.date('ymd').'.xlsx';

		//检查文件是否存在
		if (file_exists($filename)) {

			return $this->set_response([
				'status' => FALSE,
				'message' => '重复写入'
			], REST_Controller::HTTP_OK);

		} else {

			if (file_put_contents($filename, $output, FILE_APPEND)) {
				$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
				$reader->setReadDataOnly(true);
				$spreadsheet = $reader->load($filename);
				$sheet = $spreadsheet->getActiveSheet();
				$highestRow = $sheet->getHighestRow();  // 最大行数
				$highestColumn = $sheet->getHighestColumn(); // 最大列数
				$highestColumn = "I";

				// 把下标变成数字,如A-I分别对应1-9
				$highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
				if($highestRow <= 1){ // 因为students.xlsx表格数据是从第三行开始的
					return $this->set_response([
						'status' => FALSE,
						'message' => '写入失败'
					], REST_Controller::HTTP_OK);
				}

				//设置表获取条件
				$result = $this->db->where('module', "库存")->get('setting')->result_array();
//				var_dump($result);exit;

				//组装数据数组
				$data = array();
				for($row = 2;$row <= $highestRow;$row++){
					$tempData['item_id'] = $sheet->getCellByColumnAndRow(1,$row)->getValue();
					$tempData['item_status'] = $sheet->getCellByColumnAndRow(2,$row)->getValue();
					$tempData['item_url'] = $sheet->getCellByColumnAndRow(3,$row)->getValue();
					$tempData['year_season'] = $sheet->getCellByColumnAndRow(4,$row)->getValue();
					$tempData['item_title'] = $sheet->getCellByColumnAndRow(5,$row)->getValue();
					$tempData['item_price'] = $sheet->getCellByColumnAndRow(6,$row)->getValue();
					$tempData['item_discoun_price'] = $sheet->getCellByColumnAndRow(7,$row)->getValue();
					$tempData['item_stock'] = $sheet->getCellByColumnAndRow(8,$row)->getValue();
					$tempData['mercha_coding'] = $sheet->getCellByColumnAndRow(9,$row)->getValue();
					$tempData['date_time'] = date('Y-m-d 00:00:00');
					$tempData['store'] = $store;
					$tempData['positive_price'] = "";
					$data[] = $tempData;
				}

				//添加数据库
				$this->db->insert_batch('item_stock', $data);

				//填充正特价字段
				foreach ($result as $key => $value) {
					if ($value['last_condit'] != '其他') {
						$this->db->query('update item_stock set positive_price = "'.$value['result'].'" where year_season '.$value['judge'].' "'.$value['last_condit'].'" and date_time = "'.date('Y-m-d 00:00:00').'"');
					} else {
						$this->db->query('update item_stock set positive_price = "'.$value['result'].'" where positive_price = "" and date_time = "'.date('Y-m-d 00:00:00').'"');
					}
				}

				return $this->response(true, REST_Controller::HTTP_OK);
			}
		}
		return $this->response(false, REST_Controller::HTTP_OK);
	}

	/**
	 * 匹配数据 最新
	 * @throws \PhpOffice\PhpSpreadsheet\Exception
	 * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
	 */
	public function match_post()
	{
        $data = $this->post('data');
        echo $data;
        $data = (array)json_decode($data);
        $store = $data['store'];

		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$data['href']);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		$output = curl_exec($ch);
		curl_close($ch);

		$filename = 'data/'.$store.'match'.date('ymd').'.xlsx';

		//检查文件是否存在
		if (file_exists($filename)) {

			return $this->set_response([
				'status' => FALSE,
				'message' => '重复写入'
			], REST_Controller::HTTP_OK);

		} else {

			if (file_put_contents($filename, $output, FILE_APPEND)) {

				$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
				$reader->setReadDataOnly(true);
				$spreadsheet = $reader->load($filename);
				$sheet = $spreadsheet->getActiveSheet();
				$highestRow = $sheet->getHighestRow();  // 最大行数
				$highestColumn = $sheet->getHighestColumn(); // 最大列数
				$highestColumn = "I";
				// 把下标变成数字,如A-I分别对应1-9
				$highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
				if($highestRow <= 1){ // 因为students.xlsx表格数据是从第三行开始的

					return $this->set_response([
						'status' => FALSE,
						'message' => '写入失败'
					], REST_Controller::HTTP_OK);

				}

				//设置表获取条件
				$result = $this->db->where('module', "库存")->get('setting')->result_array();
//				var_dump($result);exit;

				//组装数据数组
				$data = array();
				for($row = 2;$row <= $highestRow;$row++){
					$tempData['item_id'] = $sheet->getCellByColumnAndRow(1,$row)->getValue();
					$tempData['year_season'] = $sheet->getCellByColumnAndRow(2,$row)->getValue();
					$tempData['tag_price'] = $sheet->getCellByColumnAndRow(3,$row)->getValue();
					$tempData['item_coding'] = $sheet->getCellByColumnAndRow(4,$row)->getValue();
                    $tempData['store'] = $store;
					$data[] = $tempData;
				}

				$where_in = [];
				foreach ($data as $key => $value) {
					$where_in[] = $value['item_id'];
				}
				$match = $this->db->where_in('item_id', $where_in)->get('match_data')->result_array();
//				print_r($match);exit;
				$search_in = [];
				foreach ($match as $key => $value) {
					$search_in[] = $value['item_id'];
				}

				$insert_data = [];
				foreach($data as $key => $value) {
					if (!in_array($value['item_id'], $search_in)) {
						$insert_data[] = $value;
					}
				}

				//添加数据库
				if ($insert_data) {
					$info = $this->db->insert_batch('match_data', $insert_data);
				}else{
					$info = 0;
				}

				return $this->response($info, REST_Controller::HTTP_OK);
			}
		}

		return $this->response(false, REST_Controller::HTTP_OK);
	}

	/**
	 * 流量数据
	 * @throws \PhpOffice\PhpSpreadsheet\Exception
	 * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
	 */
	public function flow_post()
	{
	    $data = $this->post('data');
	    echo $data;
	    $data = (array)json_decode($data);
		$date_time = $data['date'];
		$url = "https://sycm.taobao.com/flow/gray/excel.do?spm=a21ag.11910098.0.0.298c410cm3Xl8a&_path_=v3/excel/shop/source&device=2&dateType=day&dateRange=".$date_time."|".$date_time."&belong=all";
        $store = $data['store'];
		$cookie = $this->db->where('store', $store)->order_by('id', 'DESC')->get('cookie_data')->row_array();

		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_COOKIE, $cookie['cookie_str']);
		$output = curl_exec($ch);
		curl_close($ch);

		$filename = 'data/'.$store.'liuliang'.$date_time.'.xls';


		if (empty($output)) {
			return $this->set_response([
				'status' => FALSE,
				'message' => '空数据'
			], REST_Controller::HTTP_OK);
		}

		//检查文件是否存在
		if (file_exists($filename)) {

			return $this->set_response([
				'status' => FALSE,
				'message' => '重复请求'
			], REST_Controller::HTTP_OK);

		} else {
			if (file_put_contents ( $filename, $output, FILE_APPEND )) {
				$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
				$reader->setReadDataOnly(true);
				$spreadsheet = $reader->load($filename);
				$sheet = $spreadsheet->getActiveSheet();
				$highestRow = $sheet->getHighestRow();  // 最大行数
				$highestColumn = $sheet->getHighestColumn(); // 最大列数
				$highestColumn = "AD";
				// 把下标变成数字,如A-I分别对应1-30
				$highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);

				if($highestRow <= 7){ // 因为students.xlsx表格数据是从第五行开始的
					return $this->set_response([
						'status' => FALSE,
						'message' => '写入失败'
					], REST_Controller::HTTP_OK);
				}

				//组装数据数组
				$data = array();
				for($row = 7;$row <= $highestRow;$row++){
					$tempData['flow_source'] = $sheet->getCellByColumnAndRow (1, $row)->getValue ();
					$tempData['source_detail'] = $sheet->getCellByColumnAndRow (2, $row)->getValue ();
					$tempData['visitors'] = $sheet->getCellByColumnAndRow (3, $row)->getValue ();
					$tempData['visitors_change'] = $sheet->getCellByColumnAndRow (4, $row)->getValue ();
					$tempData['order_amt'] = $sheet->getCellByColumnAndRow (5, $row)->getValue ();
					$tempData['order_amt_change'] = $sheet->getCellByColumnAndRow (6, $row)->getValue ();
					$tempData['order_buy'] = $sheet->getCellByColumnAndRow (7, $row)->getValue ();
					$tempData['order_buy_change'] = $sheet->getCellByColumnAndRow (8, $row)->getValue ();
					$tempData['order_conversion'] = $sheet->getCellByColumnAndRow (9, $row)->getValue ();
					$tempData['order_conversion_change'] = $sheet->getCellByColumnAndRow (10, $row)->getValue ();
					$tempData['pay_amt'] = $sheet->getCellByColumnAndRow (11, $row)->getValue ();
					$tempData['pay_amt_change'] = $sheet->getCellByColumnAndRow (12, $row)->getValue ();
					$tempData['pay_buy'] = $sheet->getCellByColumnAndRow (13, $row)->getValue ();
					$tempData['pay_buy_change'] = $sheet->getCellByColumnAndRow (14, $row)->getValue ();
					$tempData['pay_conversion'] = $sheet->getCellByColumnAndRow (15, $row)->getValue ();
					$tempData['pay_conversion_change'] = $sheet->getCellByColumnAndRow (16, $row)->getValue ();
					$tempData['customer_price'] = $sheet->getCellByColumnAndRow (17, $row)->getValue ();
					$tempData['customer_price_change'] = $sheet->getCellByColumnAndRow (18, $row)->getValue ();
					$tempData['uv_value'] = $sheet->getCellByColumnAndRow (19, $row)->getValue ();
					$tempData['uv_value_change'] = $sheet->getCellByColumnAndRow (20, $row)->getValue ();
					$tempData['attention_store_buy'] = $sheet->getCellByColumnAndRow (21, $row)->getValue ();
					$tempData['attention_store_buy_change'] = $sheet->getCellByColumnAndRow (22, $row)->getValue ();
					$tempData['collect_item_buy'] = $sheet->getCellByColumnAndRow (23, $row)->getValue ();
					$tempData['collect_item_buy_change'] = $sheet->getCellByColumnAndRow (24, $row)->getValue ();
					$tempData['plus_num'] = $sheet->getCellByColumnAndRow (25, $row)->getValue ();
					$tempData['plus_num_change'] = $sheet->getCellByColumnAndRow (26, $row)->getValue ();
					$tempData['new_visitors'] = $sheet->getCellByColumnAndRow (27, $row)->getValue ();
					$tempData['new_visitors_change'] = $sheet->getCellByColumnAndRow (28, $row)->getValue ();
					$tempData['date_time'] = date("Y-m-d 00:00:00", strtotime("-1 day")); //日期
                    $tempData['store'] = $store;
					$data[] = $tempData;
				}
				//添加数据库
				$info = $this->db->insert_batch('flow_data', $data);

				return $this->response($info, REST_Controller::HTTP_OK);
			}
		}

	}

	/**
	 * 存储当前会话cookie
	 */
	public function cookie_post()
	{
	    $data = $this->post('data');
	    echo $data;
        $data = (array)json_decode($data);

        $store = $data['sto']->store;

		$cookie_obj = $data['cookie'];
		$cookie_str = "";
		foreach ($cookie_obj as $key => $value) {
			$cookie_str .= $value->name."=".$value->value."; ";
		}
		$cookie_str = rtrim(rtrim($cookie_str, " "), ";");

		$data_r['cookie_str'] = $cookie_str;
		$data_r['store'] = $store;
		if($cookie_str) {
			$info = $this->db->insert('cookie_data', $data_r);
		}else{
			return $this->set_response([
				'status' => FALSE,
				'message' => '数据为空'
			], REST_Controller::HTTP_OK);
		}
		return $this->response($info, REST_Controller::HTTP_OK);
	}

	/**
     * 三个评分指标
     */
	public function score_post()
    {
        $data = $this->post('data');
        echo $data;
        $data = (array)json_decode($data);

        $update = [
            'desc_same_aver' => $data['percentage']['0'],
            'service_same_aver' => $data['percentage'][1],
            'logistic_same_aver' => $data['percentage'][2]
        ];
        $info = $this->db->where('store', $data['store'])->where('date_time',date("Y-m-d 00:00:00", strtotime("-1 day")))->update('day_take_num', $update);

        return $this->response($info, REST_Controller::HTTP_OK);
    }
}
